home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / PROGTOOL / SPIN.ZIP;1 / SPININFO.TXT < prev   
Encoding:
Text File  |  1994-07-27  |  4.4 KB  |  117 lines

  1. …ÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕª
  2. ∫                        Spin Demo Information                         ∫
  3. ∫                            July 20, 1994                             ∫
  4. ∫                           Peter M. Freese                            ∫
  5. »ÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕÕº
  6.  
  7.  
  8. Running Spin
  9. ~~~~~~~~~~~~
  10. Use left Ctrl and Alt to scale in and out, use right Ctrl and Alt to
  11. spin the bitmap.
  12.  
  13. You can change the bitmap by replaced SPIN.PCX with your own image.
  14. The image must be a 64x64 bitmap.
  15.  
  16.  
  17. How does Spin work?
  18. ~~~~~~~~~~~~~~~~~~~
  19. Spin achieves very fast dynamic bitmap scaling and rotation by using a
  20. destination centric copy in a tightly coded assembly inner loop.  By
  21. "destination centric", I mean that rather than determine where each
  22. pixel in the source bitmaps should go, it looks at each point in the
  23. destination and determines where the pixel comes from.
  24.  
  25. The algorithm basically needs three bits of information in order to
  26. draw the scaled and rotated bitmap: the starting texture coordinate to
  27. draw at the upper left of the destination (startingU, startingV), the
  28. texture deltas to move right through the destination (dU, dV), and the
  29. texture deltas to move down through the destination (dU2, dV2).
  30.  
  31. The algorithm works basically as follows:
  32.  
  33.     // calculate deltas
  34.     dU = cos(angle) * scale;
  35.     dV = sin(angle) * scale;
  36.  
  37.     dU2 = cos(angle - 90) * scale;
  38.     dV2 = cos(angle - 90) * scale;
  39.  
  40.     rowU = startingU;
  41.     rowV = startingV;
  42.  
  43.     for ( y = 0; y < height; y++)
  44.     {
  45.         u = rowU;
  46.         v = rowV;
  47.  
  48.         for ( x = 0; x < width; x++)
  49.         {
  50.             dest[x,y] = texture[u,v];
  51.             u += dU;
  52.             v += dV;
  53.         }
  54.  
  55.         rowU += dU2;
  56.         rowV += dV2;
  57.     }
  58.  
  59. This is a simplified version of the algorithm, some details are not
  60. shown in order to make this pseudo code easier to understand:
  61.  
  62. The startingU and startingV values are calculated from the distance
  63. from the center of the rotation to the upper left of the destination by
  64. using the delta values.
  65.  
  66. The vertical delta are aspect adjusted for VGA mode 0x13.
  67.  
  68. The texture coordinates are all implemented as fixed point numbers.
  69. The radix points are set so that texture coordinates overflow (wrap) at
  70. the edges of the source texture.  This gives the most precision, and
  71. speeds the wrapping.  In order for this to work, the size of the source
  72. texture must be a power of two.  In the case of the example, I used a
  73. 64x64 texture.
  74.  
  75. If you understand this algorithm, you should see that the scaling
  76. process is almost happenstance -- the deltas are merely adjusted by a
  77. factor.  If you just want to do scaling without rotation, you should be
  78. able to greatly simplify this algorithm.
  79.  
  80. If you are wondering how tightly this algorithm can be coded in
  81. assembly, the Spin inner loop uses just 6 instructions per pixel, not
  82. counting the destination write (the inner loop is unrolled to write 4
  83. pixels at a time).  You should be able to do just scaling in less.
  84.  
  85.  
  86. 486 cache misses
  87. ~~~~~~~~~~~~~~~~
  88. The spin speed suffers when dealing with large bitmaps.  The problem is
  89. that when the bitmap is larger than the CPU cache (8K on a 486), there
  90. will be cache misses while drawing the bitmap.  This is aggravated when
  91. the bitmap is rotated near 90 or 270 degrees, since there will likely
  92. be many cache misses over the course of a scan line.  Theoretically,
  93. this problem can be minimized by using a version of the image already
  94. rotated by 90 degrees for certain quadrants of rotation.  Ultimately,
  95. however, even this will be of little help as the image gets larger than
  96. 256x256.  How much of an impact do cache misses have?  Run SPIN and
  97. turn of your 486 CPU's internal cache (most 486 turbo buttons don't
  98. actually change the CPU speed, but just disable the cache).  On my
  99. machine the frame rate drops from 90 to about 16.
  100.  
  101. Unfortunately, there's not much you can do about cache misses except be
  102. aware of their consequence and code accordingly.
  103.  
  104.  
  105. More questions?
  106. ~~~~~~~~~~~~~~~
  107. If you have any further questions about how Spin works, post me a
  108. message in the Game Design section of the Gamers forum.  I prefer a
  109. forum message rather than private email since others will get the
  110. benefit of the reply.
  111.  
  112. Thanks for your feedback.
  113.  
  114. Peter Freese
  115. 74170,543 (CompuServe)
  116. codezero@halcyon.com
  117.